package com.leyou.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.common.pojo.R;
import com.leyou.common.utils.AliOss;
import com.leyou.mall.data.dto.MallGoodsCommentDto;
import com.leyou.mall.data.dto.MallGoodsDto;
import com.leyou.mall.data.entity.MallGoodsComment;
import com.leyou.mall.data.entity.MallGoodsPicture;
import com.leyou.mall.data.vo.MallCommentVo;
import com.leyou.mall.data.vo.MallGoodsComVo;
import com.leyou.mall.mapper.MallGoodsCommentMapper;
import com.leyou.mall.mapper.MallGoodsMapper;
import com.leyou.mall.service.MallGoodsCommentService;
import com.leyou.mall.service.MallGoodsPictureService;
import com.leyou.mall.service.MallGoodsService;
import com.leyou.order.data.entity.OrderGoodsItem;
import com.leyou.order.service.OrderGoodsItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xhh
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/leyou.mall/mall-goods-comment")
public class MallGoodsCommentController {
    @Autowired
    private MallGoodsMapper mallGoodsMapper;
    @Autowired
    private MallGoodsCommentMapper mallGoodsCommentMapper;
    @Autowired
    private MallGoodsCommentService mallGoodsCommentService;
    @Autowired
    private MallGoodsPictureService mallGoodsPictureService;
    @Autowired
    private MallGoodsService mallGoodsService;
    @Autowired
    private AliOss aliOss;
    @Autowired
    private OrderGoodsItemService orderGoodsItemService;
    /**
    * todo 商品评价列表
    * @param vo
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/7 9:16
    * @deprecated 根据商品id获取
    */
    @RequestMapping(value = "/goodsCommentByGoodsId",method = RequestMethod.POST)
    public R goodsCommentByGoodsId(MallGoodsComVo vo){
        Integer goodsId=vo.getGoodsId();
        //获取评论列表
        List<MallGoodsCommentDto> mallGoodsCommentDtos = mallGoodsMapper.commentList(goodsId);
        //获取购买人数
        MallGoodsDto mallGoodsDto = mallGoodsMapper.goodsOne(goodsId);
        int buyCount=mallGoodsCommentDtos.size();
        //获取综合评分
        int MarkNum=0,MarkPeople=mallGoodsCommentDtos.size();
        for (MallGoodsCommentDto mallGoodsCommentDto : mallGoodsCommentDtos) {
            MarkNum+=mallGoodsCommentDto.getMark();
        }
        //保留一位小数
        BigDecimal bigDecimal=new BigDecimal(0);
        if(MarkNum>0 && MarkPeople>0){
            bigDecimal=new BigDecimal((float)MarkNum/(float)MarkPeople);
        }

        double point = bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        //再获取分页的列表
        Page<MallGoodsCommentDto> page=new Page(vo.getPage(),vo.getLimit());
        Page page1 = mallGoodsMapper.commentList(page, goodsId);
        //获取商品主图片和商品名称
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",goodsId);
        String goodsName=mallGoodsService.getOne(queryWrapper).getName();
        queryWrapper.clear();
        queryWrapper.eq("goods_id",goodsId);
        queryWrapper.eq("main_pic",1);
        MallGoodsPicture one = mallGoodsPictureService.getOne(queryWrapper);
        String url;
        if(one==null){
            url="";
        }else{
            url=mallGoodsPictureService.getOne(queryWrapper).getUrl();
        }

        return R.ok().data("data",page1).
                data("buyCount",buyCount).
                data("point",point).
                data("goodsName",goodsName).
                data("url",url);
    }

    /**
     * todo 商品评价获取指定个数
     * @param id
     * @param num
     * @return com.leyou.common.pojo.R
     * @author xhh
     * @data 2022/7/7 9:16
     * @deprecated 根据商品id获取
     */
    @RequestMapping(value = "/phoneShowComment",method = RequestMethod.POST)
    public R phoneShowComment(@RequestParam("goodsId")Integer id,@RequestParam("num")Integer num){
        Page page=new Page(1,num);
        Page<MallGoodsCommentDto> page1 = mallGoodsMapper.commentList(page, id);
        return R.ok().data("list",page1.getRecords());
    }

    /**
    * todo 用户评论
    * @param vo
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/20 20:18
    * @deprecated 先mall_goods_comment添加评论 ,并将返回id关联order_goods 的comment_id 并更改state
    */
    @Transactional
    @RequestMapping(value = "/toComment",method = RequestMethod.POST)
    public R toComment(MallCommentVo vo){

        System.err.println(vo);
        MallGoodsComment mallComment1=(MallGoodsComment)vo;
        System.err.println(mallComment1);

        String upload = aliOss.upload(vo.getFile());
        if(!upload.equals("") && upload!=null){

            //mall_goods_comment添加评论
            MallGoodsComment mallComment=(MallGoodsComment)vo;
            mallComment.setPicUrl(upload);
            OrderGoodsItem byId = orderGoodsItemService.getById(vo.getOrderId());
            mallComment.setGoodsId(byId.getSpuId());
            boolean save = mallGoodsCommentService.save(mallComment);
            //将返回id关联order_goods 的comment_id
            OrderGoodsItem goodsItem=new OrderGoodsItem();
            goodsItem.setGoodsCommentId(mallComment.getId());
            goodsItem.setState(5);
            LambdaQueryWrapper<OrderGoodsItem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            boolean update = orderGoodsItemService.update(goodsItem, lambdaQueryWrapper);
            if(save && update){
                return R.ok();
            }else{
                return R.error();
            }
        }else{
            return R.error();
        }
    }
}

